package com.wind.taobao.api.trade.shipping.convert
{
	import com.wind.taobao.api.trade.shipping.model.Area;
	import com.wind.taobao.core.util.TaobaoConstants;

	import mx.collections.ArrayCollection;


	/**
	 * Area转换器
	 * @author fxy
	 *
	 */
	public class AreaConvert
	{
		/**
		 * 转换Area集合
		 */
		public static function convertAreas(value:Object, type:String="json"):ArrayCollection
		{
			var areas:ArrayCollection=new ArrayCollection();
			if (value != null)
			{
				if (type == TaobaoConstants.JSON)
				{
					if ((value as Array).length > 0)
					{
						for (var i:uint=0; i < (value as Array).length; i++)
						{
							areas.addItem(convertArea((value as Array)[i]));
						}
					}
				}
				else
				{
					//TODO
				}
			}
			return areas;
		}

		/**
		 * 转换单个Area对象
		 * value:XML 或 JSON
		 */
		public static function convertArea(value:Object, type:String="json", area:Area=null):Area
		{
			if (area == null)
				area=new Area();
			if (value == null)
				return area;
			if (type == TaobaoConstants.XML)
			{
				area=convertXMLToArea(XML(value), area);
			}
			else
			{
				area=convertJSONToArea(value, area);
			}
			return area;
		}

		/**
		 * JSON转换为Area
		 */
		private static function convertJSONToArea(json:Object, area:Area):Area
		{
			if (json.hasOwnProperty(TaobaoConstants.AREA_ID))
			{
				area.area_id=json[TaobaoConstants.AREA_ID];
			}
			if (json.hasOwnProperty(TaobaoConstants.AREA_NAME))
			{
				area.area_name=json[TaobaoConstants.AREA_NAME];
			}
			if (json.hasOwnProperty(TaobaoConstants.AREA_TYPE))
			{
				area.area_type=json[TaobaoConstants.AREA_TYPE];
			}
			if (json.hasOwnProperty(TaobaoConstants.PARENT_ID))
			{
				area.parent_id=json[TaobaoConstants.PARENT_ID];
			}
			if (json.hasOwnProperty(TaobaoConstants.ZIP))
			{
				area.zip=json[TaobaoConstants.ZIP];
			}
			return area;
		}



		/**
		 * XML转换为Area
		 */
		private static function convertXMLToArea(value:XML, area:Area):Area
		{
			return area;
		}
	}
}